#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

static pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;

void *Thread_CallBake(void * argv)
{
    //把收到的参数加1
    int * number = argv;
    //加索  当当前线程持有这个锁  别的线程 就不能获取当这个锁
    pthread_mutex_lock(&counter_mutex);
    *number += 1;
    //释放一个锁
    pthread_mutex_unlock(&counter_mutex);
    return NULL;
}



int main(int argc, char const *argv[])
{
    int num = 0;
    //用来保存线程标识的数组
    pthread_t thread[20000];
    //创建2000个线程
    for (int i = 0; i < 20000; i++)
    {
       pthread_create(thread+i,NULL,Thread_CallBake,&num);
    }

    //回收线程
    for(int i = 0 ; i < 20000;i++)
    {
        pthread_join(thread[i],NULL);
    }

    //答应num
    printf("num = %d\n",num);
    
    return 0;
}

